package com.minicat.server;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求信息封装成Request对象
 */
public class Request {

    private String method;

    private String url;

    private String protocol;

    private String protocolVersion;

    private String host;

    private String connection;

    private String userAgent;

    private String accept;

    private String cookie;

    private InputStream inputStream;

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
            while (count == 0) {
                count = inputStream.available();
            }
            byte[] bytes = new byte[count];
            inputStream.read(bytes);
            String inputStr = new String(bytes);
        /**
         * GET /zx HTTP/1.1
         * Host: localhost:8071
         * Connection: keep-alive
         * Upgrade-Insecure-Requests: 1
         * User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
         * Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp;q = 0.8
         * Accept - Encoding:gzip, deflate, sdch, br
         * Accept - Language:zh - CN, zh;q = 0.8
         * Cookie:JSESSIONID = CA6D0849E7C2A2E4CB22439BC9AC5B4D
         */
        String[] inputStrs = inputStr.split("\n");
        String firstLineStr = inputStrs[0];
        String[] strings = firstLineStr.split(" ");
        this.method = strings[0];
        this.url = strings[1];
        this.protocol = strings[2].split("/")[0];
        this.protocolVersion = strings[2].split("/")[1];
        for (int i = 1;i < inputStrs.length;i++){
            String[] strs = inputStrs[i].split(":");
            switch (strs[0]){
                case "Host":
                    this.host = strs[1].trim();
                    break;
                case "Connection":
                    this.connection = strs[1].trim();
                    break;
                case "User-Agent":
                    this.userAgent = strs[1].trim();
                    break;
                case "Cookie":
                    this.cookie = strs[1].trim();
                    break;
                case "Accept":
                    this.accept = strs[1].trim();
                    break;
            }
        }
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getProtocolVersion() {
        return protocolVersion;
    }

    public void setProtocolVersion(String protocolVersion) {
        this.protocolVersion = protocolVersion;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public String toString() {
        return "Request{" +
                "method='" + method + '\'' +
                ", url='" + url + '\'' +
                ", protocol='" + protocol + '\'' +
                ", protocolVersion='" + protocolVersion + '\'' +
                ", host='" + host + '\'' +
                ", connection='" + connection + '\'' +
                ", userAgent='" + userAgent + '\'' +
                ", accept='" + accept + '\'' +
                ", cookie='" + cookie + '\'' +
                ", inputStream=" + inputStream +
                '}';
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getConnection() {
        return connection;
    }

    public void setConnection(String connection) {
        this.connection = connection;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public String getAccept() {
        return accept;
    }

    public void setAccept(String accept) {
        this.accept = accept;
    }

    public String getCookie() {
        return cookie;
    }

    public void setCookie(String cookie) {
        this.cookie = cookie;
    }
}
